﻿using GalaSoft.MvvmLight.Command;
using BuyingSpree_HPV.Help;
using BuyingSpree_HPV.Model;
using BuyingSpree_HPV.VM.Base;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows;

namespace BuyingSpree_HPV.VM
{
    public class SelectUserViewModel: DialogProcess
    {
        private ObservableCollection<Member> gridModelList;

        public ObservableCollection<Member> GridModelList
        {
            get { return gridModelList; }
            set { gridModelList = value; RaisePropertyChanged(); }
        }
        private RelayCommand<Member> _editCommand;

        public RelayCommand<Member> EditCommand
        {
            get
            {
                if (_editCommand == null)
                    _editCommand = new RelayCommand<Member>(t => Edit(t));
                return _editCommand;
            }
        }
        
        private void Edit(Member data)
        {
            try
            {
                GridModelList.ToList().ForEach((ary) => { if (ary.id != data.id) ary.IsSelected = false; });
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                LoggerHelper.log.Info(DateTime.Now.ToString() + "异常-" + ex.StackTrace + "\r\n描述-" + ex.Message);
            }
        }

        public  void InitUi()
        {
            GridModelList = new ObservableCollection<Member>();
            var members = HttpService.GetMembers();
            if (members!=null && members.Count!=0)
            {
                members.ForEach((ary) =>
                {
                    GridModelList.Add(ary);
                });
            }
        }

        public override void Save()
        {
            try
            {
                var SelectData = GridModelList.FirstOrDefault(s => s.IsSelected);
                if (SelectData!=null)
                {
                    if (SelectData.sex==1)
                    {
                        MessageBox.Show($"当前选择用户{SelectData.name}为男性哟！！！");
                        return;
                    }
                    Config.HeaderConfig.idCard = SelectData.idCardNo;
                    Config.HeaderConfig.memberId = SelectData.id;
                    Config.HeaderConfig.memberName = SelectData.name;
                }
              
                base.Save();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                LoggerHelper.log.Info(DateTime.Now.ToString() + "异常-" + ex.StackTrace + "\r\n描述-" + ex.Message);
            }
          
        }

    }
}
